package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketStorage;
import com.cskaoyan.market.db.domain.MarketStorageExample;
import com.cskaoyan.market.db.mapper.MarketStorageMapper;
import com.cskaoyan.market.service.AdminStorageService;
import com.cskaoyan.market.util.MybatisUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;


public class AdminStorageServiceImpl implements AdminStorageService {


    @Override
    public int create(MarketStorage marketStorage) {

        return MybatisUtils.getSession().getMapper(MarketStorageMapper.class).insertSelective(marketStorage);

    }

    @Override
    public List<MarketStorage> list(String id, String key, String type, String size, String addTime, String updateTime, String name, String url, String deleted, String page, String limit) {

        SqlSession session = MybatisUtils.getSession();
        MarketStorageMapper mapperStorageMapper = session.getMapper(MarketStorageMapper.class);
        MarketStorageExample marketStorageExample = new MarketStorageExample();
        MarketStorageExample.Criteria criteria = marketStorageExample.createCriteria();

        //根据key和对象名称来进行模糊查询
        if (!StringUtils.isEmpty(key)&&StringUtils.isEmpty(name)){
            criteria.andKeyLike("%"+key+"%");
        }
        if (!StringUtils.isEmpty(name)&&StringUtils.isEmpty(key)){
            criteria.andNameLike("%"+name+"%");
        }
        if (!StringUtils.isEmpty(key)&&!StringUtils.isEmpty(name)){
            criteria.andKeyLike("%"+key+"%");
            criteria.andNameLike("%"+name+"%");
        }

        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));

        List<MarketStorage> marketStorages =mapperStorageMapper.selectByExample(marketStorageExample);

        return marketStorages;

    }

    @Override
    public int delete(MarketStorage marketStorage) {

        SqlSession session = MybatisUtils.getSession();

        int i = session.getMapper(MarketStorageMapper.class).deleteByPrimaryKey(marketStorage.getId());

        session.commit();
        session.close();
        return i;


    }


}
